using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Common;

using MakeIT.Framework.Data;
using MakeIT.SGI.ERP.Entidades;

namespace MakeIT.SGI.ERP.Negocios
{
	public partial class SgiDAOT : DAOT
	{

        public TitpagVO getTitpag(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao, string nrDocOri, int? idBanco)
		{
			TitpagVO titpagVO = null;
			List<TitpagVO> listTitpagVOs = getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, situacao, nrDocOri,  idBanco);
			if (listTitpagVOs != null && listTitpagVOs.Count > 0)
			{
				titpagVO = listTitpagVOs[0];
			}
			return titpagVO;
		}

        public TitpagVO getTitpag(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao, string nrDocOri)
        {
            return getTitpag(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, situacao, nrDocOri, null);
        }

        public TitpagVO getTitpag(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao)
        {
            return getTitpag(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, situacao, null);
        }

        public TitpagVO getTitpag(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto)
        {
            return getTitpag(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, null);
        }

        public TitpagVO getTitpag(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta)
        {
            return getTitpag(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, null);
        }

        public TitpagVO getTitpag(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto)
        {
            return getTitpag(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, null);
        }

        public TitpagVO getTitpag(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor)
        {
            return getTitpag(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, null);
        }

        public TitpagVO getTitpag(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa)
        {
            return getTitpag(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, null);
        }

        public TitpagVO getTitpag(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor)
        {
            return getTitpag(idTitPag, nrDoc, idFilial, idCtaCor, null);
        }

        public TitpagVO getTitpag(int? idTitPag, string nrDoc, int? idFilial)
        {
            return getTitpag(idTitPag, nrDoc, idFilial, null);
        }

        public TitpagVO getTitpag(int? idTitPag, string nrDoc)
        {
            return getTitpag(idTitPag, nrDoc, null);
        }

        public TitpagVO getTitpag(int? idTitPag)
        {
            return getTitpag(idTitPag, null);
        }

		public List<TitpagVO> getTitpags()
		{
			return getTitpags(null);
		}

        public List<TitpagVO> getTitpags(int? idTitPag)
        {
            return getTitpags(idTitPag, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc)
        {
            return getTitpags(idTitPag, nrDoc, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, situacao, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao, string nrDocOri)
		{
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, situacao, nrDocOri, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao, string nrDocOri, int? idBanco)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, situacao, nrDocOri, idBanco, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, situacao, nrDocOri, idBanco, vctoDe, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, situacao, nrDocOri, idBanco, vctoDe, vctoAte, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, situacao, nrDocOri, idBanco, vctoDe, vctoAte, pgtoDe, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe, DateTime? pgtoAte)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, situacao, nrDocOri, idBanco, vctoDe, vctoAte, pgtoDe, pgtoAte, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe, DateTime? pgtoAte, decimal? valorDe)
        {
            return getTitpags(idTitPag, nrDoc, idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, situacao, nrDocOri, idBanco, vctoDe, vctoAte, pgtoDe, pgtoAte, valorDe, null);
        }

        public List<TitpagVO> getTitpags(int? idTitPag, string nrDoc, int? idFilial, int? idCtaCor, int? idCaixa, int? idFornecedor, int? idFormaPgto, int? idPlnCta, int? idCCusto, char? situacao, string nrDocOri, int? idBanco, DateTime? vctoDe, DateTime? vctoAte, DateTime? pgtoDe, DateTime? pgtoAte, decimal? valorDe, decimal? valorAte)
		{
			string where = "WHERE ";
			StringBuilder query = new StringBuilder();
            
            query.AppendLine("SELECT ttp.idTitPag as ttp_idTitPag, ttp.idFilial as ttp_idFilial, fil.filial as fil_filial, ttp.idCtaCor as ttp_idCtaCor, ctc.ctaCor as ctc_ctaCor, ttp.idCaixa as ttp_idCaixa, cxa.caixa as cxa_caixa, ttp.idFornecedor as ttp_idFornecedor, ttp.idFormaPgto as ttp_idFormaPgto, fpg.formaPgto as fpg_formaPgto, ttp.idPlnCta as ttp_idPlnCta, plc.plnCta as plc_plnCta, ttp.idCCusto as ttp_idCCusto, cct.cCusto as cct_cCusto, ttp.nrDoc as ttp_nrDoc, ttp.nrDocOri as ttp_nrDocOri, ttp.obs as ttp_obs, ttp.vcto as ttp_vcto, ttp.pgto as ttp_pgto, ttp.valor as ttp_valor, ttp.valorJuroC as ttp_valorJuroC, ttp.valorDescC as ttp_valorDescC, ttp.valorPago as ttp_valorPago, ttp.valorSaldo as ttp_valorSaldo, ttp.situacao as ttp_situacao, ttp.idTitPagOri as ttp_idTitPagOri, ttp.cadastro as ttp_cadastro, ttp.cadastradoPor as ttp_cadastradoPor, ttp.edicao as ttp_edicao, ttp.editadoPor as ttp_editadoPor  ");
			query.AppendLine("FROM titpag as ttp ");
            query.AppendLine("  LEFT OUTER JOIN filial as fil on ttp.idFilial=fil.idFilial ");
            query.AppendLine("      LEFT OUTER JOIN ctacor as ctc on ttp.idCtaCor=ctc.idCtaCor");
            query.AppendLine("          LEFT OUTER JOIN banco as ban on ctc.idBanco=ban.idBanco");
            query.AppendLine("              LEFT OUTER JOIN caixa as cxa on ttp.idCaixa=cxa.idCaixa");
            query.AppendLine("                  LEFT OUTER JOIN fornecedor as frn on ttp.idFornecedor=frn.idFornecedor ");
            query.AppendLine("                      LEFT OUTER JOIN formapgto as fpg on ttp.idFormaPgto=fpg.idFormaPgto");
            query.AppendLine("                          LEFT OUTER JOIN plncta as plc on ttp.idPlnCta=plc.idPlnCta");
            query.AppendLine("                              LEFT OUTER JOIN ccusto as cct on ttp.idCCusto=cct.idCCusto  ");
			if (idTitPag != null)
			{
				query.AppendLine(where + "ttp.idTitPag=@idTitPag ");
				where = "AND ";
			}
			if (idFilial != null)
			{
				query.AppendLine(where + "ttp.idFilial=@idFilial ");
				where = "AND ";
			}
			if (idCtaCor != null)
			{
				query.AppendLine(where + "ttp.idCtaCor=@idCtaCor ");
				where = "AND ";
			}
			if (idCaixa != null)
			{
				query.AppendLine(where + "ttp.idCaixa=@idCaixa ");
				where = "AND ";
			}
			if (idFornecedor != null)
			{
                query.AppendLine(where + "ttp.idFornecedor=@idFornecedor ");
				where = "AND ";
			}
			if (idFormaPgto != null)
			{
				query.AppendLine(where + "ttp.idFormaPgto=@idFormaPgto ");
				where = "AND ";
			}
			if (idPlnCta != null)
			{
				query.AppendLine(where + "ttp.idPlnCta=@idPlnCta ");
				where = "AND ";
			}
			if (idCCusto != null)
			{
				query.AppendLine(where + "ttp.idCCusto=@idCCusto ");
				where = "AND ";
			}
			if (nrDoc != null)
			{
				query.AppendLine(where + "ttp.nrDoc LIKE @nrDoc ");
				where = "AND ";
			}
			if (situacao != null)
			{
				query.AppendLine(where + "ttp.situacao=@situacao ");
				where = "AND ";
			}
            if (nrDocOri != null)
            {
                query.AppendLine(where + "ttp.nrDocOri=@nrDocOri ");
                where = "AND ";
            }
            if (idBanco != null)
            {
                query.AppendLine(where + "ctc.idBanco=@idBanco ");
                where = "AND ";
            }
            if (vctoDe != null)
            {
                query.AppendLine(where + "ttp.vcto>=@vctoDe ");
                where = "AND ";
            }
            if (vctoAte != null)
            {
                query.AppendLine(where + "ttp.vcto<=@vctoAte ");
                where = "AND ";
            }
            if (pgtoDe != null)
            {
                query.AppendLine(where + "ttp.pgto>=@pgtoDe ");
                where = "AND ";
            }
            if (pgtoAte != null)
            {
                query.AppendLine(where + "ttp.pgto<=@pgtoAte ");
                where = "AND ";
            }
            if (valorDe != null)
            {
                query.AppendLine(where + "ttp.valor>=@valorDe ");
                where = "AND ";
            }
            if (valorAte != null)
            {
                query.AppendLine(where + "ttp.valor<=@valorAte ");
                where = "AND ";
            }


            query.AppendLine("ORDER BY ttp.vcto ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			if (idTitPag!= null) cmd.Parameters.Add(getParameter(cmd, "@idTitPag", idTitPag));
			if (idFilial!= null) cmd.Parameters.Add(getParameter(cmd, "@idFilial", idFilial));
			if (idCtaCor!= null) cmd.Parameters.Add(getParameter(cmd, "@idCtaCor", idCtaCor));
			if (idCaixa!= null) cmd.Parameters.Add(getParameter(cmd, "@idCaixa", idCaixa));
			if (idFornecedor!= null) cmd.Parameters.Add(getParameter(cmd, "@idFornecedor", idFornecedor));
			if (idFormaPgto!= null) cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", idFormaPgto));
			if (idPlnCta!= null) cmd.Parameters.Add(getParameter(cmd, "@idPlnCta", idPlnCta));
			if (idCCusto!= null) cmd.Parameters.Add(getParameter(cmd, "@idCCusto", idCCusto));
			if (nrDoc!= null) cmd.Parameters.Add(getParameter(cmd, "@nrDoc", "%"+nrDoc+"%"));
            if (situacao != null) cmd.Parameters.Add(getParameter(cmd, "@situacao", situacao));
            if (nrDocOri != null) cmd.Parameters.Add(getParameter(cmd, "@nrDocOri", nrDocOri));
            if (idBanco != null) cmd.Parameters.Add(getParameter(cmd, "@idBanco", idBanco));

            if (vctoDe != null) 
            {
                DateTime data = new DateTime(vctoDe.Value.Year, vctoDe.Value.Month, vctoDe.Value.Day, 0, 0, 0);
                cmd.Parameters.Add(getParameter(cmd, "@vctoDe", data)); 
            }

            if (vctoAte != null)
            {
                DateTime data = new DateTime(vctoAte.Value.Year, vctoAte.Value.Month, vctoAte.Value.Day, 23, 59, 59);
                cmd.Parameters.Add(getParameter(cmd, "@vctoAte", data));
            }

            if (pgtoDe != null)
            {
                DateTime data = new DateTime(pgtoDe.Value.Year, pgtoDe.Value.Month, pgtoDe.Value.Day, 0, 0, 0);
                cmd.Parameters.Add(getParameter(cmd, "@pgtoDe", data));
            }

            if (pgtoAte != null)
            {
                DateTime data = new DateTime(pgtoAte.Value.Year, pgtoAte.Value.Month, pgtoAte.Value.Day, 23, 59, 59);
                cmd.Parameters.Add(getParameter(cmd, "@pgtoAte", data));
            }

            if (valorDe != null) cmd.Parameters.Add(getParameter(cmd, "@valorDe", valorDe));
            if (valorAte != null) cmd.Parameters.Add(getParameter(cmd, "@valorAte", valorAte));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();

				List<TitpagVO> listTitpag = new List<TitpagVO>();

				while (reader.Read())
				{
					TitpagVO titpagVO = TitpagVO.CreateVO();
					titpagVO.idTitPag = reader.GetInt32(reader.GetOrdinal("ttp_idTitPag"));
					titpagVO.idFilial = reader.GetInt32(reader.GetOrdinal("ttp_idFilial"));
					if (!reader.IsDBNull(reader.GetOrdinal("fil_filial")))
						titpagVO.idFilialVO.filial = reader.GetString(reader.GetOrdinal("fil_filial"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_idCtaCor")))
						titpagVO.idCtaCor = reader.GetInt32(reader.GetOrdinal("ttp_idCtaCor"));
					if (!reader.IsDBNull(reader.GetOrdinal("ctc_ctaCor")))
						titpagVO.idCtaCorVO.ctaCor = reader.GetString(reader.GetOrdinal("ctc_ctaCor"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_idCaixa")))
						titpagVO.idCaixa = reader.GetInt32(reader.GetOrdinal("ttp_idCaixa"));
					if (!reader.IsDBNull(reader.GetOrdinal("cxa_caixa")))
						titpagVO.idCaixaVO.caixa = reader.GetString(reader.GetOrdinal("cxa_caixa"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_idFornecedor")))
						titpagVO.idFornecedor = reader.GetInt32(reader.GetOrdinal("ttp_idFornecedor"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_idFormaPgto")))
						titpagVO.idFormaPgto = reader.GetInt32(reader.GetOrdinal("ttp_idFormaPgto"));
					if (!reader.IsDBNull(reader.GetOrdinal("fpg_formaPgto")))
						titpagVO.idFormaPgtoVO.formaPgto = reader.GetString(reader.GetOrdinal("fpg_formaPgto"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_idPlnCta")))
						titpagVO.idPlnCta = reader.GetInt32(reader.GetOrdinal("ttp_idPlnCta"));
					if (!reader.IsDBNull(reader.GetOrdinal("plc_plnCta")))
						titpagVO.idPlnCtaVO.plnCta = reader.GetString(reader.GetOrdinal("plc_plnCta"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_idCCusto")))
						titpagVO.idCCusto = reader.GetInt32(reader.GetOrdinal("ttp_idCCusto"));
					if (!reader.IsDBNull(reader.GetOrdinal("cct_cCusto")))
						titpagVO.idCCustoVO.cCusto = reader.GetString(reader.GetOrdinal("cct_cCusto"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_nrDoc")))
						titpagVO.nrDoc = reader.GetString(reader.GetOrdinal("ttp_nrDoc"));
                    if (!reader.IsDBNull(reader.GetOrdinal("ttp_nrDocOri")))
                        titpagVO.nrDocOri = reader.GetString(reader.GetOrdinal("ttp_nrDocOri"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_obs")))
						titpagVO.obs = reader.GetString(reader.GetOrdinal("ttp_obs"));
					titpagVO.vcto = reader.GetDateTime(reader.GetOrdinal("ttp_vcto"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_pgto")))
						titpagVO.pgto = reader.GetDateTime(reader.GetOrdinal("ttp_pgto"));
					titpagVO.valor = reader.GetDecimal(reader.GetOrdinal("ttp_valor"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_valorJuroC")))
						titpagVO.valorJuroC = reader.GetDecimal(reader.GetOrdinal("ttp_valorJuroC"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_valorDescC")))
						titpagVO.valorDescC = reader.GetDecimal(reader.GetOrdinal("ttp_valorDescC"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_valorPago")))
						titpagVO.valorPago = reader.GetDecimal(reader.GetOrdinal("ttp_valorPago"));
					if (!reader.IsDBNull(reader.GetOrdinal("ttp_valorSaldo")))
						titpagVO.valorSaldo = reader.GetDecimal(reader.GetOrdinal("ttp_valorSaldo"));
					titpagVO.situacao = reader.GetChar(reader.GetOrdinal("ttp_situacao"));
					titpagVO.cadastro = reader.GetDateTime(reader.GetOrdinal("ttp_cadastro"));
					titpagVO.cadastradoPor = reader.GetString(reader.GetOrdinal("ttp_cadastradoPor"));
					titpagVO.edicao = reader.GetDateTime(reader.GetOrdinal("ttp_edicao"));
					titpagVO.editadoPor = reader.GetString(reader.GetOrdinal("ttp_editadoPor"));


					listTitpag.Add(titpagVO);
				}
				reader.Close();
				return listTitpag;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void setTitpag(TitpagVO titpagVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("UPDATE titpag ");
            query.AppendLine("SET idFilial=@idFilial, idCtaCor=@idCtaCor, idCaixa=@idCaixa, idFornecedor=@idFornecedor, idFormaPgto=@idFormaPgto, idPlnCta=@idPlnCta, idCCusto=@idCCusto, nrDoc=@nrDoc, nrDocOri=@nrDocOri, obs=@obs, vcto=@vcto, pgto=@pgto, valor=@valor, valorJuroC=@valorJuroC, valorDescC=@valorDescC, valorPago=@valorPago, valorSaldo=@valorSaldo, situacao=@situacao,  cadastro=@cadastro, cadastradoPor=@cadastradoPor, edicao=@edicao, editadoPor=@editadoPor ");
			query.AppendLine("WHERE idTitPag=@idTitPag ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idTitPag", titpagVO.idTitPag));
			cmd.Parameters.Add(getParameter(cmd, "@idFilial", titpagVO.idFilial));
			if (titpagVO.idCtaCor == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCtaCor", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCtaCor", titpagVO.idCtaCor));
			if (titpagVO.idCaixa == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCaixa", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCaixa", titpagVO.idCaixa));
			if (titpagVO.idFornecedor == null)
				cmd.Parameters.Add(getParameter(cmd, "@idFornecedor", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idFornecedor", titpagVO.idFornecedor));
			if (titpagVO.idFormaPgto == null)
				cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", titpagVO.idFormaPgto));
			if (titpagVO.idPlnCta == null)
				cmd.Parameters.Add(getParameter(cmd, "@idPlnCta", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idPlnCta", titpagVO.idPlnCta));
			if (titpagVO.idCCusto == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCCusto", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCCusto", titpagVO.idCCusto));
			if (titpagVO.nrDoc == null)
				cmd.Parameters.Add(getParameter(cmd, "@nrDoc", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@nrDoc", titpagVO.nrDoc));
            if (titpagVO.nrDocOri == null)
                cmd.Parameters.Add(getParameter(cmd, "@nrDocOri", System.DBNull.Value));
            else
                cmd.Parameters.Add(getParameter(cmd, "@nrDocOri", titpagVO.nrDocOri));
			if (titpagVO.obs == null)
				cmd.Parameters.Add(getParameter(cmd, "@obs", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@obs", titpagVO.obs));
			cmd.Parameters.Add(getParameter(cmd, "@vcto", titpagVO.vcto));
			if (titpagVO.pgto == null)
				cmd.Parameters.Add(getParameter(cmd, "@pgto", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@pgto", titpagVO.pgto));
			cmd.Parameters.Add(getParameter(cmd, "@valor", titpagVO.valor));
			if (titpagVO.valorJuroC == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorJuroC", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorJuroC", titpagVO.valorJuroC));
			if (titpagVO.valorDescC == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorDescC", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorDescC", titpagVO.valorDescC));
			if (titpagVO.valorPago == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorPago", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorPago", titpagVO.valorPago));
			if (titpagVO.valorSaldo == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorSaldo", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorSaldo", titpagVO.valorSaldo));
			cmd.Parameters.Add(getParameter(cmd, "@situacao", titpagVO.situacao));
			cmd.Parameters.Add(getParameter(cmd, "@cadastro", titpagVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@cadastradoPor", titpagVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@edicao", titpagVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@editadoPor", titpagVO.editadoPor));

			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }

		}

		public long newTitpag(TitpagVO titpagVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("INSERT INTO titpag ");
            query.AppendLine("  ( idFilial, idCtaCor, idCaixa, idFornecedor, idFormaPgto, idPlnCta, idCCusto, nrDoc, nrDocOri, obs, vcto, pgto, valor, valorJuroC, valorDescC, valorPago, valorSaldo, situacao, cadastro, cadastradoPor, edicao, editadoPor ) ");
            query.AppendLine("VALUES ( @idFilial, @idCtaCor, @idCaixa, @idFornecedor, @idFormaPgto, @idPlnCta, @idCCusto, @nrDoc, @nrDocOri, @obs, @vcto, @pgto, @valor, @valorJuroC, @valorDescC, @valorPago, @valorSaldo, @situacao, @cadastro, @cadastradoPor, @edicao, @editadoPor ) ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idFilial", titpagVO.idFilial));
			if (titpagVO.idCtaCor == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCtaCor", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCtaCor", titpagVO.idCtaCor));
			if (titpagVO.idCaixa == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCaixa", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCaixa", titpagVO.idCaixa));
			if (titpagVO.idFornecedor == null)
				cmd.Parameters.Add(getParameter(cmd, "@idFornecedor", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idFornecedor", titpagVO.idFornecedor));
			if (titpagVO.idFormaPgto == null)
				cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", titpagVO.idFormaPgto));
			if (titpagVO.idPlnCta == null)
				cmd.Parameters.Add(getParameter(cmd, "@idPlnCta", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idPlnCta", titpagVO.idPlnCta));
			if (titpagVO.idCCusto == null)
				cmd.Parameters.Add(getParameter(cmd, "@idCCusto", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@idCCusto", titpagVO.idCCusto));
			if (titpagVO.nrDoc == null)
				cmd.Parameters.Add(getParameter(cmd, "@nrDoc", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@nrDoc", titpagVO.nrDoc));
            if (titpagVO.nrDocOri == null)
                cmd.Parameters.Add(getParameter(cmd, "@nrDocOri", System.DBNull.Value));
            else
                cmd.Parameters.Add(getParameter(cmd, "@nrDocOri", titpagVO.nrDocOri));
			if (titpagVO.obs == null)
				cmd.Parameters.Add(getParameter(cmd, "@obs", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@obs", titpagVO.obs));
			cmd.Parameters.Add(getParameter(cmd, "@vcto", titpagVO.vcto));
			if (titpagVO.pgto == null)
				cmd.Parameters.Add(getParameter(cmd, "@pgto", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@pgto", titpagVO.pgto));
			cmd.Parameters.Add(getParameter(cmd, "@valor", titpagVO.valor));
			if (titpagVO.valorJuroC == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorJuroC", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorJuroC", titpagVO.valorJuroC));
			if (titpagVO.valorDescC == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorDescC", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorDescC", titpagVO.valorDescC));
			if (titpagVO.valorPago == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorPago", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorPago", titpagVO.valorPago));
			if (titpagVO.valorSaldo == null)
				cmd.Parameters.Add(getParameter(cmd, "@valorSaldo", System.DBNull.Value));
			else
				cmd.Parameters.Add(getParameter(cmd, "@valorSaldo", titpagVO.valorSaldo));
			cmd.Parameters.Add(getParameter(cmd, "@situacao", titpagVO.situacao));
			cmd.Parameters.Add(getParameter(cmd, "@cadastro", titpagVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@cadastradoPor", titpagVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@edicao", titpagVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@editadoPor", titpagVO.editadoPor));


			try
			{
				cmd.ExecuteNonQuery();

				long newID = 0;
				if (tipo.Equals(DBType.MSSql))
				{
					cmd.CommandText = "SELECT IDENT_CURRENT('titpag') AS 'Identity' ";
					DbDataReader reader = cmd.ExecuteReader();
					reader.Read();
					newID = (long)reader.GetDecimal(reader.GetOrdinal("Identity"));
					reader.Close();
				}
				else if (tipo.Equals(DBType.MySql))
				{
					newID = ((MySql.Data.MySqlClient.MySqlCommand)cmd).LastInsertedId;
				}
				else if (tipo.Equals(DBType.ODBC))
				{
					//TODO implementar
				}
				else if (tipo.Equals(DBType.ORACLE))
				{
					//TODO implementar
				}
				return newID;
			}
			catch (Exception ex) { throw ex; }

		}

		public void delTitpag(TitpagVO titpagVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("DELETE FROM titpag ");
			query.AppendLine("WHERE idTitPag=@idTitPag ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idTitPag", titpagVO.idTitPag));


			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }

		}
		public int getCountTitpagxIdFilial(int? idFilial)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idFilial) as ocorrencias FROM titpag WHERE IdFilial=@IdFilial ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdFilial", idFilial));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitpagxIdCtaCor(int? idCtaCor)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idCtaCor) as ocorrencias FROM titpag WHERE IdCtaCor=@IdCtaCor ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdCtaCor", idCtaCor));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitpagxIdCaixa(int? idCaixa)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idCaixa) as ocorrencias FROM titpag WHERE IdCaixa=@IdCaixa ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdCaixa", idCaixa));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitpagxIdFornecedor(int? idFornecedor)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idFornecedor) as ocorrencias FROM titpag WHERE IdFornecedor=@IdFornecedor ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdFornecedor", idFornecedor));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitpagxIdFormaPgto(int? idFormaPgto)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idFormaPgto) as ocorrencias FROM titpag WHERE IdFormaPgto=@IdFormaPgto ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdFormaPgto", idFormaPgto));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitpagxIdPlnCta(int? idPlnCta)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idPlnCta) as ocorrencias FROM titpag WHERE IdPlnCta=@IdPlnCta ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdPlnCta", idPlnCta));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
		public int getCountTitpagxIdCCusto(int? idCCusto)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idCCusto) as ocorrencias FROM titpag WHERE IdCCusto=@IdCCusto ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdCCusto", idCCusto));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
	}
}
